package com.dime.physical.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.dime.basic.api.RemoteGroupService;
import com.dime.basic.api.domain.DimeBasegroup;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dime.physical.mapper.DimePhysicalflowMapper;
import com.dime.physical.domain.DimePhysicalflow;
import com.dime.physical.service.IDimePhysicalflowService;
/**
 * 人员流程Service业务层处理
 *
 * @author Tiny
 * @date 2023-10-30
 */
@Service
public class DimePhysicalflowServiceImpl implements IDimePhysicalflowService
{
    @Autowired
    private DimePhysicalflowMapper dimePhysicalflowMapper;


    @Autowired
    private RemoteGroupService remoteGroupService;
    /**
     * 查询人员流程
     *
     * @param id 人员流程主键
     * @return 人员流程
     */
    @Override
    public DimePhysicalflow selectDimePhysicalflowById(String id)
    {
        return dimePhysicalflowMapper.selectDimePhysicalflowById(id);
    }

    /**
     * 查询人员流程列表
     *
     * @param dimePhysicalflow 人员流程
     * @return 人员流程
     */
    @Override
    public List<DimePhysicalflow> selectDimePhysicalflowList(DimePhysicalflow dimePhysicalflow)
    {
        return dimePhysicalflowMapper.selectDimePhysicalflowList(dimePhysicalflow);
    }

    /**
     * 新增人员流程
     *
     * @param dimePhysicalflow 人员流程
     * @return 结果
     */
    @Override
    public int insertDimePhysicalflow(DimePhysicalflow dimePhysicalflow)
    {
        dimePhysicalflow.setCreateTime(DateUtils.getNowDate());
        return dimePhysicalflowMapper.insertDimePhysicalflow(dimePhysicalflow);
    }

    /**
     * 修改人员流程
     *
     * @param dimePhysicalflow 人员流程
     * @return 结果
     */
    @Override
    public int updateDimePhysicalflow(DimePhysicalflow dimePhysicalflow)
    {
        dimePhysicalflow.setUpdateTime(DateUtils.getNowDate());
        return dimePhysicalflowMapper.updateDimePhysicalflow(dimePhysicalflow);
    }

    /**
     * 批量删除人员流程
     *
     * @param ids 需要删除的人员流程主键
     * @return 结果
     */
    @Override
    public int deleteDimePhysicalflowByIds(String[] ids)
    {
        return dimePhysicalflowMapper.deleteDimePhysicalflowByIds(ids);
    }

    /**
     * 删除人员流程信息
     *
     * @param id 人员流程主键
     * @return 结果
     */
    @Override
    public int deleteDimePhysicalflowById(String id)
    {
        return dimePhysicalflowMapper.deleteDimePhysicalflowById(id);
    }

    /**
     * 通过用户编号获取去重后的小组
     *
     * @param userId 用户编号
     * @return
     */
    @Override
    public List<DimePhysicalflow> selectDistinctDimePhysiacaflowListByUserId(String userId) {
        List<DimePhysicalflow> dimePhysicalflows = getDimePhysiacaflowListByUserId(userId);
        List<DimePhysicalflow> distinctList = dimePhysicalflows.stream()
                .collect(Collectors.groupingBy(DimePhysicalflow::getBaseGroupId))
                .values().stream()
                .map(x -> x.get(0))
                .collect(Collectors.toList());
        return distinctList;
    }

    /**
     * 通过用户编号获取全部小组
     *
     * @param userId 用户编号
     * @return
     */
    @Override
    public List<DimePhysicalflow> getDimePhysiacaflowListByUserId(String userId) {
        List<DimePhysicalflow> dimePhysicalflows = new ArrayList<>();
        List<DimePhysicalflow> _dimePhysicalflows = dimePhysicalflowMapper.selectDimePhysicalflowList(new DimePhysicalflow());
        R<List<DimeBasegroup>> remoteGroupServiceGroupByUserId = remoteGroupService.getGroupByUserId(userId);
        if (remoteGroupServiceGroupByUserId.getCode() != R.FAIL) {
            List<DimeBasegroup> groups = remoteGroupServiceGroupByUserId.getData();
            dimePhysicalflows = groups.stream()
                    .flatMap(group -> _dimePhysicalflows.stream()
                            .filter(x -> Objects.equals(x.getBaseGroupId(), group.getId()))
                            .peek(x -> x.setGroupName(group.getGroupName())))
                    .collect(Collectors.toList());
        }
        return dimePhysicalflows;
    }
}
